package com.zscat.mallplus.cms.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.active.entity.CmsActive;
import com.zscat.mallplus.active.service.ICmsActiceService;
import com.zscat.mallplus.cms.entity.CmsFavorite;
import com.zscat.mallplus.cms.entity.CmsSubject;
import com.zscat.mallplus.cms.mapper.CmsFavoriteMapper;
import com.zscat.mallplus.cms.service.ICmsFavoriteService;
import com.zscat.mallplus.cms.service.ICmsSubjectService;
import com.zscat.mallplus.pms.entity.PmsProduct;
import com.zscat.mallplus.pms.mapper.PmsProductMapper;
import com.zscat.mallplus.pms.service.IPmsProductService;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-06-15
 */
@Service
public class CmsFavoriteServiceImpl extends ServiceImpl<CmsFavoriteMapper, CmsFavorite> implements ICmsFavoriteService {

	@Resource
	private CmsFavoriteMapper productCollectionRepository;
	@Resource
	private PmsProductMapper productMapper;
	@Resource
	private ICmsActiceService acticeService;
    @Resource
    private CmsFavoriteMapper favoriteMapper;

    @Resource
    private IUmsMemberService memberService;

    @Resource
    private IPmsProductService productService;

    @Resource
    private ICmsSubjectService subjectService;

//	@Override
	public int addProduct(CmsFavorite productCollection) {
		int count = 0;
		CmsFavorite query = new CmsFavorite();
		query.setObjId(productCollection.getObjId());
		query.setMemberId(productCollection.getMemberId());
		query.setType(productCollection.getType());
		CmsFavorite findCollection = productCollectionRepository.selectOne(new QueryWrapper<>(query));
		if (findCollection == null) {
			productCollection.setAddTime(new Date());
			productCollection.setMemberId(productCollection.getMemberId());
			productCollection.setObjId(productCollection.getObjId());
			if (productCollection.getType() == 1) {
//                PmsProduct subject = productMapper.selectById(productCollection.getObjId());
//                subject.setId(productCollection.getObjId());
//                subject.setGiftPoint(subject.getGiftPoint() + 1);
//                //更新到数据库
//                productMapper.updateById(subject);
			} else if (productCollection.getType() == 2 || productCollection.getType() == 3
					|| productCollection.getType() == 4) {
				CmsActive active = acticeService.getById(productCollection.getObjId());
				active.setId(productCollection.getObjId());
				active.setLikeCount(active.getLikeCount() == null ? 1 : active.getLikeCount() + 1);
				// 更新到数据库
				acticeService.updateById(active);
				productCollection.setStoreId(active.getStoreId());
				productCollection.setName(active.getActiveTitle());
			} else if (productCollection.getType() == 5) {
				CmsSubject subject = subjectService.getById(productCollection.getObjId());
				subject.setId(productCollection.getObjId());
				subject.setLikeCount(subject.getLikeCount() == null ? 1 : subject.getLikeCount() + 1);
				// 更新到数据库
				subjectService.updateById(subject);
				productCollection.setStoreId(subject.getStoreId());
				productCollection.setName(subject.getTitle());
			}
			productCollectionRepository.insert(productCollection);
			count = 1;
		} else {
			if (productCollection.getType() == 1) {
//				PmsProduct subject = productMapper.selectById(productCollection.getObjId());
//				subject.setId(productCollection.getObjId());
//				subject.setGiftPoint(subject.getGiftPoint() - 1);
//				// 更新到数据库
//				productMapper.updateById(subject);
			} else if (productCollection.getType() == 2 || productCollection.getType() == 3
					|| productCollection.getType() == 4) {
				CmsActive active = acticeService.getById(productCollection.getObjId());
				active.setId(productCollection.getObjId());
				active.setLikeCount(
						(active.getLikeCount() == null || active.getLikeCount() == 0) ? 0 : active.getLikeCount() - 1);
				// 更新到数据库
				acticeService.updateById(active);
			} else if (productCollection.getType() == 5) {
				CmsSubject subject = subjectService.getById(productCollection.getObjId());
				subject.setId(productCollection.getObjId());
				subject.setLikeCount((subject.getLikeCount() == null || subject.getLikeCount() == 0) ? 0
						: subject.getLikeCount() - 1);
				// 更新到数据库
				subjectService.updateById(subject);
			}
			return productCollectionRepository.delete(new QueryWrapper<>(query));
		}
		return count;
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object saveOrCancel(CmsFavorite favoriteParam) {
    	System.out.println(favoriteParam.getType());
        try {
            if (ValidatorUtils.empty(favoriteParam.getObjId())) {
                return new CommonResult().failed("商品或文章ID不能为空");
            }
            if (ValidatorUtils.empty(favoriteParam.getType())) {
                return new CommonResult().failed("点赞类别不能为空");
            }

            UmsMember currentMember = memberService.getNewCurrentMember();
            CmsFavorite query = new CmsFavorite();
            if (null != currentMember) {
                favoriteParam.setMemberId(currentMember.getId());
            } else {
                return new CommonResult().failed("请先登录");
            }

            // 查询当前用户是否已完成点赞
            CmsFavorite favorite = favoriteMapper.selectOne(new QueryWrapper<CmsFavorite>().eq("obj_id", favoriteParam.getObjId()).eq("member_id", currentMember.getId()).eq("type", favoriteParam.getType()));
            if (null == favorite) {  // 点赞
                favoriteParam.setAddTime(new Date());
                favoriteMapper.insert(favoriteParam);
                if (favoriteParam.getType() == 1) {
                    PmsProduct product = productService.getById(favoriteParam.getObjId());
                    product.setId(favoriteParam.getObjId());
                    product.setGiftPoint(product.getGiftPoint() + 1);
                    productService.updateById(product);
                } else if (favoriteParam.getType() == 5) {
                    CmsSubject subject = subjectService.getById(favoriteParam.getObjId());
                    subject.setId(favoriteParam.getObjId());
                    subject.setForwardCount(subject.getCollectCount() + 1);
                    subjectService.updateById(subject);
                }else if(favoriteParam.getType() ==6 || favoriteParam.getType() ==7 || favoriteParam.getType() ==8) {
                	CmsActive active = acticeService.getById(favoriteParam.getObjId());
                	active.setId(favoriteParam.getObjId());
                	active.setLikeCount(active.getLikeCount() == null? 1 : active.getLikeCount() + 1);
                	acticeService.updateById(active);
                }
                return new CommonResult().success("点赞成功");
            } else {  // 取消点赞
                if (favoriteParam.getType() == 1) {
                    PmsProduct subject = productService.getById(favoriteParam.getObjId());
                    subject.setId(favoriteParam.getObjId());
                    subject.setGiftPoint(subject.getGiftPoint() - 1);
                    productService.updateById(subject);
                }
                if (favoriteParam.getType() == 2) {
                    CmsSubject subject = subjectService.getById(favoriteParam.getObjId());
                    subject.setId(favoriteParam.getObjId());
                    subject.setForwardCount(subject.getCollectCount() - 1);
                    subjectService.updateById(subject);
                }
                favoriteMapper.delete(new QueryWrapper<>(query));
                return new CommonResult().success("取消点赞成功");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

//	@Override
//	public List<CmsFavorite> listCollect(Long memberId) {
//		return productCollectionRepository.selectList(new QueryWrapper<CmsFavorite>().eq("member_id", memberId));
//	}
    @Override
    public List<CmsFavorite> listProduct(Long memberId, int type) {
        return favoriteMapper.selectList(new QueryWrapper<CmsFavorite>().eq("member_id", memberId).eq("type", type));
    }

    @Override
    public List<CmsFavorite> listCollect(Long memberId) {
        return favoriteMapper.selectList(new QueryWrapper<CmsFavorite>().eq("member_id", memberId));
    }
}
